var express = require('express');
var userdb = require('../model/usersSchema');
var router = express.Router();

/* GET home page. */
// router.get('/login', function(req, res, next) {
//   res.render('login');
// });

router.post('/login',function(req,res,next){
  //接收前端的数据
  res.header("Access-Control-Allow-Origin","*");
  res.header("Access-Control-Allow-Headers","X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  var username=req.body.username;
  var password=req.body.password;
  //2、连接数据库，先判断用户名是否存在。不存在就报错;存在就跳转
  userdb.find({"username":username,"password":password},function(data){
    if(data.length>0){
      // //1、记录session 后端session  前段cookie
      // req.session.username=username;
      // //cookie
      // var d=new Date();
      // d.setDate(d.getDate()+7);
      // res.cookie("username",username,{expires:d});
      //2、跳转
      res.json({
        "checked":"1"
      });
    }else{
      res.json({
        "checked":"0"
      })
    };
  });
})

module.exports = router;
